package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemEnterpriseConfigMenuBinding;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.recyclerview.adapter.PageRecycleAdapter;
import com.swgk.core.util.MToast;

import java.util.List;

public class EnterpriseConfigMenuAdapter extends PageRecycleAdapter<EnterpriseRecEntity> {

    public EnterpriseConfigMenuAdapter(Context context, List<EnterpriseRecEntity> data) {
        super(context, R.layout.item_enterprise_config_menu, data);
    }

    @Override
    public void convert(ItemViewHolder holder, EnterpriseRecEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemEnterpriseConfigMenuBinding binding = (ItemEnterpriseConfigMenuBinding) holder.getmBinding();
        if (!TextUtils.isEmpty(entity.getCompanyName())) {
            if (entity.getCompanyName().length() <= 6) {
                binding.tvContent.setText(entity.getCompanyName());
            } else {
                binding.tvContent.setText(entity.getCompanyName().substring(0, 6) + "...");
            }
        }
        if (entity.isSelect()) {
            binding.ivDelete.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_config_select));
        } else {
            binding.ivDelete.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_config_add));
        }
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!entity.isSelect()) {
                    listener.onItemDelete(entity);
                }else {
                    MToast.showToast(context, "该企业已存在，请勿重复选择");
                }
            }
        });
    }

    private ItemEditClickListener listener;

    public interface ItemEditClickListener {
        void onItemDelete(EnterpriseRecEntity entity);
    }

    public void setListener(ItemEditClickListener listener) {
        this.listener = listener;
    }
}
